package com.lai.shop.controller;


import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lai.shop.entity.UserEntity;
import com.lai.shop.service.UserService;
import com.lai.shop.utils.PageUtils;
import com.lai.shop.utils.R;


/**
 * 用户表
 *
 * @author laiciqi
 * @email 602469238@qq.com
 * @date 2022-05-19 13:16:53
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查看所有用户列表")
    @ApiImplicitParam(name = "params", value = "查询参数", required = true)
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "根据id查询用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存用户")
    @ApiImplicitParam(name = "user", value = "用户实体", required = true,dataType = "UserEntity")
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user) {
        userService.adminSaveUser(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParam(name = "user", value = "用户实体", required = true,dataType = "UserEntity")
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "需要删除的id", required = true)
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 修改用户状态
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "修改用户状态")
    @ApiImplicitParam(name = "user", value = "用户实体", required = true,dataType = "UserEntity")
    @PostMapping("/updateUserStatus")
    public R updateUserStatus(@RequestBody UserEntity user) {
        //UPDATE user SET is_disable=? WHERE id=?
        boolean b = userService.updateById(user);
        return b ? R.ok() : R.error();
    }


}
